// component/common/pickerSingle/pickerSingle.js
const app = getApp();
Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的传入样式
   */
  externalClasses: ['style-extra'],

  /**
   * 组件的属性列表
   */
  properties: {
    range: {
      type: Array,
      value: []
    },
    select: {
      type: Array,
      value: []
    },
    isCustomHeader: {
      type: String,
      value: '0',
    },
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的数据监听
   */
  observers: {

  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      let t = this
      t.setData({
        height: app.globalData.navHeight,
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onMultiChange(e) {
      let t = this
      let {
        select,
      } = t.data || {}
      let {
        listIndex,
        listIn
      } = e.currentTarget.dataset
      if (!select[listIndex]) {
        select[listIndex] = []
      }
      if (select[listIndex] && t.isWhatType(select[listIndex]) !== 'Array') {
        select[listIndex] = []
      }
      if (select[listIndex].indexOf(listIn) !== -1) {
        select[listIndex].splice(select[listIndex].indexOf(listIn), 1)
      } else {
        select[listIndex].push(listIn)
      }
      t.setData({
        select,
      })
    },

    onSingleChange(e) {
      let t = this
      let {
        select,
      } = t.data || {}
      let {
        listIndex,
        listIn
      } = e.currentTarget.dataset
      if (select[listIndex] === listIn) {
        select[listIndex] = ''
      } else {
        select[listIndex] = listIn
      }
      t.setData({
        select,
      })
    },

    onMinInputBlur(e) {
      let t = this
      let {
        select,
      } = t.data || {}
      let index = e.target.dataset.index
      let value = Number(e.detail.value)
      let path = `select[${index}]`
      t.setData({
        [path]: {
          min: value || null,
          max: select && select[index] && t.isWhatType(select[index]) === 'Object' && select[index].max ? select[index].max : null
        }
      })
    },

    onMaxInputBlur(e) {
      let t = this
      let {
        select,
      } = t.data || {}
      let index = e.target.dataset.index
      let value = Number(e.detail.value)
      let path = `select[${index}]`
      t.setData({
        [path]: {
          min: select && select[index] && t.isWhatType(select[index]) === 'Object' && select[index].min ? select[index].min : null,
          max: value || null,
        }
      })
    },

    onReset() {
      let t = this
      t.setData({
        select: []
      })
    },

    onConfirm() {
      let t = this
      let {
        select,
        range
      } = t.data
      if (!t.verifyInput(select)) {
        wx.showToast({
          title: '输入有误，最大值不能小于最小值~',
          icon: 'none'
        })
        return
      }
      let result = {}
      let label = ''
      range.forEach((item, index) => {
        switch (t.isWhatType(select[index])) {
          case 'Object':
            if (item.multi) {
              result[item.id] = [select[index]]
            }
            if (item.single) {
              result[item.id] = select[index]
            }
            if (!label) {
              if (select[index].min && select[index].max) {
                label = `${select[index].min}-${select[index].max}`
              } else if (select[index].min && !select[index].max) {
                label = `${select[index].min}`
              } else if (!select[index].min && select[index].max) {
                label = `${select[index].max}`
              } else {
                label = ''
              }
            }
            break;
          case 'Array':
            let resultFilter = item.multi.filter((itx, inx) => {
              return select[index].indexOf(inx) !== -1
            })
            result[item.id] = resultFilter.map(itx => {
              return itx.value
            })
            if (!label) {
              let resultLabel = resultFilter.map(itx => {
                return itx.label
              })
              label = resultLabel.join(',')
            }
            break;
          case 'Number':
            result[item.id] = item.single[select[index]].value
            if (!label) {
              label = item.single[select[index]].label
            }
            break;
          default:
            result[item.id] = null
        }
      })
      t.triggerEvent('confirm', {
        result,
        select,
        label,
      })
      t.onMaskClose()
    },

    verifyInput(data) {
      let t = this
      let flag = true
      data.forEach(item => {
        if (t.isWhatType(item) === 'Object') {
          if (!compareSize(item.min, item.max)) {
            flag = false
          }
        }
      })
      return flag

      function compareSize(min, max) {
        if (typeof min === 'number' && typeof max === 'number') {
          if (min > max) {
            return false
          } else {
            return true
          }
        } else {
          return true
        }
      }
    },

    onMaskClose() {
      let t = this
      t.triggerEvent('close')
    },

    isWhatType(o) {
      return Object.prototype.toString.call(o).slice(8, -1)
    },

    onMove() {
      return false
    }
  }

})